<template>
  <!--业务逻辑绑定-->
  <Register v-model="formdata" @submit="onSubmit"></Register>
</template>

<script>
import Register from "@/components/form/Register.vue";
import userApi from "@/assets/api/user.js";
import { setTimeout } from "timers";

export default {
  components: {
    Register
  },
  data() {
    return {
      formdata: {
        name: "",
        pass: "",
        againpass: ""
      }
    };
  },
  methods: {
    onSubmit() {
      if (this.formdata.name == null) {
        this.$message({
          message: "请输入用户名",
          type: "warning"
        });
        return;
      }
      if (this.formdata.pass == null) {
        this.$message({
          message: "请输入密码",
          type: "warning"
        });
        return;
      }
      if (this.formdata.againpass == null) {
        this.$message({
          message: "请重复输入密码",
          type: "warning"
        });
        return;
      }
      if (this.formdata.pass != this.formdata.againpass) {
        this.$message({
          message: "两次密码不一致",
          type: "warning"
        });
        return;
      }
      userApi
        .register({
          name: this.formdata.name,
          pass: this.formdata.pass
        })
        .then(res => {
          if (res.data.success == 1) {
            this.$message({
              message: "注册成功",
              type: "success"
            });
            setTimeout(() => {
              location.href = "/";
            }, 1000);
          } else {
            this.$message.error(res.data.message);
            return;
          }
        });
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
